package com.NeuglsWorkStudio.UI.Buttons.Elements
{
	import com.NeuglsWorkStudio.Colors.RGB;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import mx.core.TextFieldAsset;
	
	/**
	 * This class used for TTipButton.<br/> 
	 * This class represent a tool tip object.
	 * @author Neugls
	 * */
	public class Tipper extends Sprite
	{
		private var _FTip:String;
		private var _FTextField:TextField=null;
		private var _FHTMLTip:Boolean=false;
		private var _FTipBackgroundColor:int=0x0;
		private var _FTextformat:TextFormat
		
		/**
		 * @param ATip The tip message,default <code>''</code>
		 * */
		public function Tipper(ATip:String='',HTMLTip:Boolean=false)
		{
			super();
			_FTip=ATip;
			_FHTMLTip=HTMLTip;
			
			init();
			Paint();
		}

		public function get HTMLTip():Boolean
		{
			return _FHTMLTip;
		}

		public function set HTMLTip(value:Boolean):void
		{
			_FHTMLTip = value;
		}

		/**
		 * Background color of the tipper*/
		public function get TipBackgroundColor():int
		{
			return _FTipBackgroundColor;
		}

		/**
		 * @private
		 */
		public function set TipBackgroundColor(value:int):void
		{
			_FTipBackgroundColor = value;
			
			Paint();
		}

		/**
		 * Tip
		 * */
		public function get Tip():String
		{
			return _FTip;
			
		}

		/**
		 * @private
		 */
		public function set Tip(value:String):void
		{
			_FTip = value;
			_FHTMLTip?_FTextField.htmlText=_FTip:_FTextField.text=_FTip;
			
			Paint();
		}
	
		protected function init():void{
			_FTextField=new TextField();
			_FTextformat=new TextFormat();
			_FTextformat.font='Tahoma';
			_FTextformat.size='12';
			_FTextformat.color=0xFFFFFF;
			_FTextField.defaultTextFormat=_FTextformat;
			_FTextField.multiline=false;
			_FTextField.wordWrap=false;
			_FTextField.mouseEnabled=false;
			_FTextField.autoSize=TextFieldAutoSize.LEFT;
			_FHTMLTip?_FTextField.htmlText=_FTip:_FTextField.text=_FTip;
		
			this.addChild(_FTextField);
		}
		
		protected function Paint():void{
			var nwidth:Number=_FTextField.textWidth+16;
			var nheight:Number=_FTextField.textHeight+8;
			var borderColor:int=RGB.AddToRGBColor(_FTipBackgroundColor,100);
			//var borderColor:int=0xFF0000;
			var triangleVerticleLength:Number=6;
			var triangleBorderLength:Number=2*6/Math.sqrt(3);
			
			var rx:Number=-nwidth/2;
			var ry:Number=-(nheight+triangleVerticleLength);
			var round:Number=4;
			
			this.graphics.clear();
			with(this.graphics){
				lineStyle(1,borderColor,1,true);
				beginFill(_FTipBackgroundColor,0.8);
				drawRoundRect(rx,ry,nwidth,nheight,2*round,2*round);
				
				//draw tip triangle
				moveTo(-(triangleBorderLength)/2,-triangleVerticleLength);
				lineTo(0,0);
				lineTo((triangleBorderLength)/2,-triangleVerticleLength);
				
				lineStyle(1,_FTipBackgroundColor,1,true);
				lineTo(-(triangleBorderLength)/2,-triangleVerticleLength);
				
				endFill();
			}
			
			_FTextField.x=rx+5;
			_FTextField.y=ry+2;
		}
		
		public function SetTextFormat(format:TextFormat):void{
			_FTextformat=format;
			_FTextField.setTextFormat(format);
		}
		
		public function GetTipTextFormat():TextFormat{
			return _FTextformat;
		}
	}
}